import requests
from requests import RequestException


class RunMethod:

    ## 定义一个执行接口请求的类
    def send_get(self, url):
        response = requests.get(url=url, timeout=20)
        try:
            if response.status_code == 200:
                r = response
                return r
            else:
                return None
        except RequestException:
            print("请求失败")
            return None

    def send_post(self, url, data):
        response = requests.post(url=url, data=data)
        try:
            if response.status_code == 200:
                r = response
                return r
            else:
                return None
        except RequestException:
            print("请求失败")
            return None

    def run_main(self, method, url, data):
        if method == "GET":
            res = self.send_get(url)
        if method == "POST":
            res = self.send_post(url, data)
        return res


if __name__ == "__main__":
    url = 'http://172.16.55.125:50042/api/common/login'
    data = {
        "name": "yunweizhuguan",
        "pwd": "123asd!@#$",
        "code": ""
    }
    r = RunMethod()
    print(r.run_main('POST', url, data).text)